import auth from '@/utils/auth'
import { ElMessage } from 'element-plus'
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/login/index.vue')
    },

    {
      path: '/',
      component: () => import('@/views/Layout.vue'),
      // // 隐藏
      // meta: { hidden: true },
      children: [
        {
          path: 'home',
          component: () => import('@/views/home/index.vue')
        },
        {
          path: 'article',
          component: () => import('@/views/article/index.vue')
        },
        {
          path: 'publish/:id?',
          component: () => import('@/views/publish/index.vue')
        }
      ]
    }
  ],
})


router.beforeEach((to, from, next) => {
  // 获取token
  const token = auth.getToken()
  // 不是访问登录 判断有没有token 跳转到登陆页面
  if (to.path !== '/login' && !token) {
    ElMessage.error('登录信息失效')
    return next('/login')
  }else{
    next()
  }
})
export default router
